package com.qulai.amazon_us.service.amazon_us.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qulai.amazon_us.domain.amazon_us.bo.AmazonUsRecordExportBo;
import com.qulai.amazon_us.domain.amazon_us.dto.AmazonUsRecordDto;
import com.qulai.common.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qulai.amazon_us.mapper.amazon_us.AmazonUsRecordMapper;
import com.qulai.amazon_us.domain.amazon_us.AmazonUsRecord;
import com.qulai.amazon_us.service.amazon_us.IAmazonUsRecordService;

/**
 * 记录明细Service业务层处理
 * 
 * @author hunan
 * @date 2025-01-05
 */
@Service
public class AmazonUsRecordServiceImpl implements IAmazonUsRecordService 
{
    @Autowired
    private AmazonUsRecordMapper amazonUsRecordMapper;

    /**
     * 查询记录明细
     * 
     * @param id 记录明细主键
     * @return 记录明细
     */
//    @Override
//    public AmazonUsRecord selectAmazonUsRecordById(Long id)
//    {
//        return amazonUsRecordMapper.selectAmazonUsRecordById(id);
//    }

    /**
     * 查询记录明细： 分表后查询
     *
     * @param id 记录明细主键
     * @return 记录明细
     */
    @Override
    public AmazonUsRecord selectAmazonUsRecordByShopAndId(AmazonUsRecord amazonUsRecord)
    {

        String recordTableName = "amazon_us_record_"+amazonUsRecord.getShop().toLowerCase();

        return amazonUsRecordMapper.selectAmazonUsRecordByShopAndId(recordTableName,amazonUsRecord);
    }


//    @Override
//    public AmazonUsRecord selectAmazonUsRecordByAsin(String asin, String shop) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("asin", asin);
//        params.put("shop", shop);
//        return amazonUsRecordMapper.selectAmazonUsRecordByAsin(params);
//    }

    @Override
    public AmazonUsRecord selectAmazonUsRecordByAsin_v2(String asin, String shop) {
        Map<String, Object> params = new HashMap<>();
        params.put("asin", asin);
        params.put("shop", shop);

        String recordTableName = "amazon_us_record_"+ shop.toLowerCase();
        return amazonUsRecordMapper.selectAmazonUsRecordByAsin_v2(recordTableName,params);
    }

    /**
     * 查询记录明细列表
     * 
     * @param amazonUsRecord 记录明细
     * @return 记录明细
     */
//    @Override
//    public List<AmazonUsRecord> selectAmazonUsRecordList(AmazonUsRecord amazonUsRecord)
//    {
//        return amazonUsRecordMapper.selectAmazonUsRecordList(amazonUsRecord);
//    }

    /**
     * 查询记录明细列表
     *
     * @param amazonUsRecord 记录明细
     * @return 记录明细
     */
    @Override
    public List<AmazonUsRecord> selectAmazonUsRecordList_v2(AmazonUsRecordDto amazonUsRecord)
    {
        List<String> shopList = new ArrayList<>();
        for (String shop : amazonUsRecord.getShop()){
            // 验证表名安全
            validateTableName(shop);
            String recordTableName = "amazon_us_record_"+shop.toLowerCase();
            shopList.add(recordTableName);
        }
        return amazonUsRecordMapper.selectAmazonUsRecordList_v2(amazonUsRecord,shopList);
    }

    @Override
    public List<AmazonUsRecordExportBo> exportAmazonUsRecordList(AmazonUsRecordDto amazonUsRecord)
    {
        List<String> shopList = new ArrayList<>();
        for (String shop : amazonUsRecord.getShop()){
            // 验证表名安全
            validateTableName(shop);
            String recordTableName = "amazon_us_record_"+shop.toLowerCase();
            shopList.add(recordTableName);
        }
        return amazonUsRecordMapper.exportAmazonUsRecordList(amazonUsRecord,shopList.get(0));
    }

    @Override
    public Long  countAmazonUsRecordList_v2(AmazonUsRecordDto amazonUsRecord)
    {
        List<String> shopList = new ArrayList<>();
        for (String shop : amazonUsRecord.getShop()){
            // 验证表名安全
            validateTableName(shop);
            String recordTableName = "amazon_us_record_"+shop.toLowerCase();
            shopList.add(recordTableName);
        }
        return amazonUsRecordMapper.countAmazonUsRecordList_v2(amazonUsRecord,shopList);
    }

    /**
     * 表名安全验证
     *
     */
    private void validateTableName(String shop) {
        if (shop == null || !shop.matches("[a-zA-Z0-9_]+")) {
            throw new IllegalArgumentException("Invalid shop name: " + shop);
        }
    }


    /**
     * 新增记录明细
     * 
     * @param amazonUsRecord 记录明细
     * @return 结果
     */
    @Override
    public int insertAmazonUsRecord(AmazonUsRecord amazonUsRecord)
    {
        amazonUsRecord.setCreateTime(DateUtils.getNowDate());
        return amazonUsRecordMapper.insertAmazonUsRecord(amazonUsRecord);
    }

    /**
     * 修改记录明细
     * 
     * @param amazonUsRecord 记录明细
     * @return 结果
     */
    @Override
    public int updateAmazonUsRecord(AmazonUsRecord amazonUsRecord)
    {
        amazonUsRecord.setUpdateTime(DateUtils.getNowDate());
        return amazonUsRecordMapper.updateAmazonUsRecord(amazonUsRecord);
    }

    /**
     * 修改记录明细
     *
     * @param amazonUsRecord 记录明细
     * @return 结果
     */
    @Override
    public int updateAmazonUsRecord_v2(AmazonUsRecord amazonUsRecord)
    {
        amazonUsRecord.setUpdateTime(DateUtils.getNowDate());
        String recordTableName = "amazon_us_record_"+amazonUsRecord.getShop().toLowerCase();
        return amazonUsRecordMapper.updateAmazonUsRecord_v2( recordTableName, amazonUsRecord);
    }

    /**
     * 批量删除记录明细
     * 
     * @param ids 需要删除的记录明细主键
     * @return 结果
     */
//    @Override
//    public int deleteAmazonUsRecordByIds(Long[] ids)
//    {
//        return amazonUsRecordMapper.deleteAmazonUsRecordByIds(ids);
//    }

    /**
     * 删除记录明细信息
     * 
     * @param id 记录明细主键
     * @return 结果
     */
    @Override
    public int deleteAmazonUsRecordById(Long id)
    {
        return amazonUsRecordMapper.deleteAmazonUsRecordById(id);
    }
}
